package views.games;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.Insets;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.Border;

import models.util.BalloonThread;
import views.util.GameColors;
import views.util.SlotPanel;

public class PallonciniMatematiciView extends GameView {

	private JPanel sky;
	private SlotPanel[] slotsArray;
	private final int MAX_BALLOONS = 6;
	private JTextField input;
	private Font font = new Font("Segoe UI", Font.PLAIN, 20);
	private JPanel cloudPanel;
	private ActionListener currentActionListener;

	public PallonciniMatematiciView() {
		super(GameColors.VIOLA, GameColors.VIOLA_CHIARO, "TEMPO ",
				"PUNTEGGIO ", "ERRORI RIMANENTI ");
		slotsArray = new SlotPanel[MAX_BALLOONS];
		sky = new JPanel();
		sky.setBackground(GameColors.VIOLA);
		sky.setLayout(new GridLayout(1, MAX_BALLOONS));
		addBalloonSlots();
		add(sky, BorderLayout.CENTER);
		cloudPanel = new JPanel() {
			public void paintComponent(Graphics g) {
				super.paintComponent(g);
				setOpaque(false);
				try {
					Image img = ImageIO.read(new File(
							"resources\\pallonciniMatematici\\clouds.png"));
					g.drawImage(img, 0, 0, null);
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		};
		Dimension dim = new Dimension(800, 180);
		cloudPanel.setPreferredSize(dim);
		add(cloudPanel, BorderLayout.NORTH);
		cloudPanel.repaint();
		cloudPanel.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		gbc.gridx = 2;
		gbc.gridy = 3;
		input = new JTextField() {
			@Override
			public void setBorder(Border border) {
			}
		};
		input.setFont(font);
		input.setOpaque(false);
		input.setHorizontalAlignment(JTextField.CENTER);
		input.setPreferredSize(new Dimension(200, 20));
		cloudPanel.add(input, gbc);
	}

	public void checkFocusMain() {
		input.requestFocus();
	}

	private void addBalloonSlots() {
		for (int i = 0; i < MAX_BALLOONS; i++) {
			slotsArray[i] = new SlotPanel();
			sky.add(slotsArray[i]);
		}
	}

	public void addActionListenerForInput(ActionListener a) {
		currentActionListener = a;
		input.addActionListener(a);
	}

	public void addBalloon(BalloonThread b) {
		slotsArray[b.getSlot()].setBalloon(b);
	}

	public void removeBalloon(int i) {
		slotsArray[i].removeBalloon();
	}

	@Override
	public void gameEnded() {
		for( SlotPanel p : slotsArray){
			
		}
		other.setVisible(false);
		gameClock.setVisible(false);
		sky.setLayout(new GridBagLayout());
		GridBagConstraints gbc = new GridBagConstraints();
		sky.removeAll();
		sky.setBackground(GameColors.VIOLA);
		JLabel end = newLabel("Partita Conclusa!", GameColors.VIOLA_CHIARO);
		gbc.gridx = 1;
		gbc.gridy = 0;
		gbc.gridwidth = 4;
		gbc.fill = GridBagConstraints.HORIZONTAL;
		gbc.insets = new Insets(0, 0, 0, 0);
		sky.add(end, gbc);
		gbc.gridy = 1;
		sky.add(score, gbc);
		JButton restart = newButton("Ricomincia");
		JButton menu = newButton("Menu Giochi");
		gbc.gridy = 2;
		gbc.gridwidth = 2;
		gbc.gridx = 0;
		sky.add(restart, gbc);
		gbc.gridx = 2;
		sky.add(menu, gbc);
		restart.addActionListener(currentActionListener);
		menu.addActionListener(currentActionListener);
		input.removeActionListener(currentActionListener);
		input.setEnabled(false);
		cloudPanel.setVisible(false);
		updateUI();
	}

}
